<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="cn.estore.entity.Order" %>
<%@ page import="cn.estore.dao.OrderDao" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>订单查询结果</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f8f9fa;
      margin: 0;
      padding: 0;
      text-align: center;
    }

    table {
      width: 80%;
      margin: 20px auto;
      border-collapse: collapse;
      box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
    }

    th, td {
      padding: 15px;
      text-align: left;
      border-bottom: 1px solid #ddd;
    }

    .back-btn {
      color: white;
      text-decoration: none;
      background-color: #6c757d;
      padding: 8px 15px;
      border-radius: 4px;
      margin-top: 20px;
      cursor: pointer;
    }

    .back-btn:hover {
      background-color: #5a6268;
    }
  </style>
</head>
<body>
<header>
  <%
    // 检查用户是否登录，这里通过查找名为"username"的Cookie来简单判断（可根据实际登录逻辑完善）
    boolean isLoggedIn = false;
    String username = "";
    Cookie[] cookies = request.getCookies();
    if (cookies!= null) {
      for (Cookie cookie : cookies) {
        if ("username".equals(cookie.getName())) {
          isLoggedIn = true;
          username = cookie.getValue();
          break;
        }
      }
    }

    if (isLoggedIn) {
      // 如果登录，显示欢迎信息
      out.println("<span id='loggedInUser'>欢迎您，" + username + "</span>");
    } else {
      // 如果未登录，重定向到登录页面
      response.sendRedirect("login.jsp");
      return;
    }
  %>
</header>
<main>
  <%
    // 获取当前用户的ID，这里假设通过查找名为"user_id"的Cookie来获取（实际需根据登录逻辑准确获取）
    int userId = 0;
    Cookie[] userCookies = request.getCookies();
    if (userCookies!= null) {
      for (Cookie cookie : userCookies) {
        if ("user_id".equals(cookie.getName())) {
          userId = Integer.parseInt(cookie.getValue());
          break;
        }
      }
    }

    OrderDao orderDao = new OrderDao();
    List<Order> orderList = orderDao.selectOrdersByUserId(userId);
    if (orderList!= null && orderList.size() > 0) {
  %>
  <table border="1">
    <tr>
      <th>订单编号</th>
      <th>商品ID</th>
      <th>购买数量</th>
      <th>实际价格</th>
      <th>总金额</th>
      <th>下单时间</th>
    </tr>
    <%
      for (Order order : orderList) {
    %>
    <tr>
      <td><%=order.getOrderNumber()%></td>
      <td><%=order.getProductId()%></td>
      <td><%=order.getProductQuantity()%></td>
      <td><%=order.getActualPrice()%></td>
      <td><%=order.getTotalAmount()%></td>
      <td><%=order.getCreateTime()%></td>
    </tr>
    <%
      }
    %>
  </table>
  <%
    } else {
      out.println("<p>您暂无订单信息。</p>");
    }
  %>
  <a href="<%=request.getContextPath()%>/selectProductDiscount.jsp" class="back-btn">返回商品列表</a>
</main>
</body>
</html>
